/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-17
*/
package com.rzico.account.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.account.entity.Receivable;
import com.rzico.account.service.ReceivableService;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 应收款控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-02-17
 */
@Api(description = "应收款接口")
@RestController
@RequestMapping("/admin/receivable")
public class ReceivableAdminController extends BaseController {

    @Autowired
    private ReceivableService receivableService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 分页查询应收款
     *
     * @return
    */
    @ApiOperation("分页查询应收款")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "memberId", value = "客户Id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "paymentPluginId", value = "付款方式", dataType = "Long", paramType = "query"),
    })

    public CommResult<Receivable> list(Date startDate, Date endDate,Long memberId,Integer status,String paymentPluginId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
            params.put("startDate", DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }
        if (memberId!=null){
            params.put("memberId",memberId);
        }
        if (paymentPluginId!=null){
            params.put("paymentPluginId",paymentPluginId);
        }
        if (status!=null){
            params.put("status",status);
        }

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Receivable> list = receivableService.selectList(params);
        PageResult<Receivable> pageResult = new PageResult<Receivable>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条应收款
     *
     * @return
    */
    @ApiOperation("查询单条应收款")
    @GetMapping("/find/{id}")
    public CommResult<Receivable> find(@PathVariable String id) {
        Receivable result = receivableService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("receivable",result);
        return CommResult.success(result);

    }

    /**
     * 保存应收款
     *
     * @param receivable
     * @return
     */
    @ApiOperation("保存应收款")
    @PostMapping("/save")
    public CommResult<Receivable> save(@RequestBody Receivable receivable) {
        int affectCount = receivableService.insert(receivable);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        Receivable result = receivableService.findById(receivable.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除应收款
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除应收款", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除应收款,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<Receivable> del(@PathVariable String ids) {

        receivableService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新应收款
     *
     * @param receivable
     * @return
     */
    @Log(desc = "更新应收款", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新应收款")
    @PostMapping("/update")
    public CommResult<Receivable> update(@RequestBody Receivable receivable) {
        int affectCount = receivableService.updateByPrimaryKeySelective(receivable);
        if (affectCount <= 0){
            return CommResult.error();
        }
        Receivable result = receivableService.findById(receivable.getId());
        return CommResult.success(result);
    }

//
//    /**
//     * 应收款收款
//     *
//     * @param receivable
//     * @return
//     */
//    @Log(desc = "应收款收款", type = Log.LOG_TYPE.UPDATE)
//    @ApiOperation("应收款收款")
//    @PostMapping("/receivable")
//    public CommResult<Receivable> receivable(@RequestBody Receivable receivable) {
//        int affectCount = receivableService.receivable(receivable);
//        if (affectCount <= 0){
//            return CommResult.error();
//        }
//        Receivable result = receivableService.findById(receivable.getId());
//        return CommResult.success(result);
//    }
//
//
}
